package com.dzl.micro;

import com.alibaba.fastjson.JSONObject;
import com.dzl.asynthread.SyncInitDataServiceImpl;
import com.dzl.base.controller.BaseController;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.pojo.query.WelfareGoodsPageQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsBaseVo;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.micro.pojo.WelfareMicroPage;
import com.dzl.micro.pojo.vo.WelfareMicroVo;
import com.dzl.micro.query.WelfareMicroPageQuery;
import com.dzl.micro.service.WelfareMicroPageService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 微页面管理 - 控制层
 * @author WangJian
 * @date 2021/8/10 0010
 */
@Slf4j
@RestController
@RequestMapping("/welfareMicroPage")
public class WelfareMicroController extends BaseController {

    @Autowired
    private WelfareMicroPageService service;
    @Autowired
    private WelfareGoodsService welfareGoodsService;
    @Autowired
    private SyncInitDataServiceImpl syncInitDataServiceImpl;

    /**
     * 获取微页面装修列表
     * @date 2021/8/28 0028 14:25
     * @author wangjian
     * @param pageId 微页面ID
     * @return com.dzl.util.JsonResult<com.dzl.micro.pojo.vo.WelfareMicroVo>
     */
    @GetMapping(value = "getMicroPageByPageId")
    public JsonResult<WelfareMicroVo> getMicroPageByPageId(Integer pageId) {
        log.info("getMicroPageByPageId in: " + pageId);
        return JsonResult.success(service.getMicroPageByPageId(pageId, CommonConstants.DATA_PORT.SERVER));
    }

    /**
     * 根据微页面装修跳转id获取该模块所选商品
     * @date 2021/10/30 11:06
     * @author liuguanjun
     * @param microId 微页面装修跳转Id
     */
    @GetMapping(value = "getGoodsListByMicroId")
    public JsonResult<List<WelfareGoodsBaseVo>> getGoodsListByMicroId(Integer microId) {
        log.info("getGoodsIdsByMicroId in: " + microId);
        WelfareGoodsPageQuery welfareGoodsPageQuery = new WelfareGoodsPageQuery();
        welfareGoodsPageQuery.setMicroId(microId);
        welfareGoodsPageQuery.setPageSize(9999);
        return JsonResult.success(welfareGoodsService.getGoodsListPageByMicroId(welfareGoodsPageQuery).getList());
    }

    /**
     * 微页面管理列表
     * @date 2021/8/10 0010 14:47
     * @author wangjian
     * @param welfareMicroPageQuery 微页面管理列表查询条件
     * @return com.dzl.util.PageUtils<com.dzl.micro.pojo.WelfareMicroPage>
     */
    @GetMapping("/getPageListData")
    public PageUtils<WelfareMicroPage> getPageListData(WelfareMicroPageQuery welfareMicroPageQuery){
        if(getAdminInfo().getAppRuleConfig() == CommonConstants.APP_RULE_CONFIG.SYNC_PLAFORM){
            throw new CommonException("企业的运营数据同步的是福力兔平台，故微页面管理功能不可使用");
        }
        welfareMicroPageQuery.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<WelfareMicroPage> pageInfo = service.getPageListData(welfareMicroPageQuery);
        return new PageUtils<> (pageInfo);
    }

    /**
     * 新增微页面
     * @date 2021/8/10 0010 14:53
     * @author wangjian
     * @param microPage 微页面信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/saveWelfareMicroPage")
    public JsonResult<Integer> saveWelfareMicroPage(@RequestBody WelfareMicroPage microPage){
        /*if (microPage.getIsHome() == null){
            return JsonResult.failWithMsg("请选择是否是首页");
        }*/
        if (EmptyUtils.isEmpty(microPage.getName())){
            return JsonResult.failWithMsg("请输入页面名称");
        }
        WelfareMicroPage page = new WelfareMicroPage();
        page.setStatus(1);
        page.setEnterpriseId(getAdminInfo().getEnterpriseId());
        page.setName(microPage.getName());
        List<WelfareMicroPage> select = service.select(page);
        if (EmptyUtils.isNotEmpty(select)){
            return JsonResult.failWithMsg("该页面已经存在，无法新增");
        }
        /*if (microPage.getIsHome() == 1){
            WelfareMicroPage welfareMicroPage = new WelfareMicroPage();
            welfareMicroPage.setStatus(1);
            welfareMicroPage.setIsHome(1);
            welfareMicroPage.setEnterpriseId(getAdminInfo().getEnterpriseId());
            if (EmptyUtils.isNotEmpty(service.select(welfareMicroPage))){
                return JsonResult.failWithMsg("已经存在首页，无法新增");
            }
        }*/
        microPage.setIsHome(CommonConstants.NO);
        microPage.setIsMovie(CommonConstants.NO);
        microPage.setStatus(1);
        microPage.setCreaterName(getAdminInfo().getUserName());
        microPage.setCreateTime(new Date());
        microPage.setEnterpriseId(getAdminInfo().getEnterpriseId());
        return JsonResult.success(service.insertSelective(microPage));
    }

    /**
     * 删除微页面
     * @date 2021/8/10 0010 14:54
     * @author wangjian
     * @param id 微页面ID
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @DeleteMapping("/deleteMicroPage")
    public JsonResult<Integer> deleteMicroPage(Integer id){
        if (EmptyUtils.isEmpty(id)){
            return JsonResult.failWithMsg("id不能为空");
        }
        WelfareMicroPage microPage = service.selectByPrimaryKey(id);
        if (microPage.getIsHome() == 1){
            return JsonResult.failWithMsg("首页不能删除");
        }
        microPage.setStatus(0);
        return JsonResult.success(service.updateByPrimaryKeySelective(microPage));
    }

    /**
     * 修改微页面备注
     * @date 2021/8/10 0010 14:58
     * @author wangjian
     * @param jsonObject 修改微页面备注的对象，id 微页面ID  remark 备注信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateMicroPageRemark")
    public JsonResult<Integer> updateMicroPageRemark(@RequestBody JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        String remark = jsonObject.getString("remark");
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请传入id");
        }
        WelfareMicroPage microPage = new WelfareMicroPage();
        microPage.setRemark(remark);
        microPage.setId(id);
        microPage.setUpdateTime(new Date());
        microPage.setUpdaterName(getAdminInfo().getUserName());
        return JsonResult.success(service.updateByPrimaryKeySelective(microPage));
    }

    /**
     * 保存页面配置
     * @date 2021/8/12 0012 11:41
     * @author wangjian
     * @param welfareMicroVo 需要保存的页面信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/saveMicroPage")
    public JsonResult<Integer> saveMicroPage(@RequestBody WelfareMicroVo welfareMicroVo){
        Integer result = service.saveMicroPage(welfareMicroVo,getAdminInfo().getUserName());
        if(result.equals(1)){
            syncInitDataServiceImpl.getMicroPageByPageId(welfareMicroVo.getPageId());
        }
        return JsonResult.success();
    }

}
